package com.skyline.jfs.common.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.skyline.jfs.cluster.model.JFSNode;
import com.skyline.jfs.cluster.model.JFSNodeConfigProper;
import com.skyline.jfs.common.logger.LoggerFactory;

/**
 * Config
 * 
 * this Config is modified from Wu's Config
 * 
 * @author Jairus Chan
 */
public class Config {

	/*
	 * constants
	 */
	private static final String SQL_CONFIG_DIR = "config/sql";
	private static final String LOCAL_FS_CONFIG_DIR = "config/jfsnode";
	private static final String MESSAGE_DIR="config/message";

	private static Properties sqlProperties = null;
	private static Properties localFSProperties = null;
	private static Properties messageProperties=null;
	
	private static List<JFSNode> clusterNodes=null;
	
	public static void config() {
		localFSProperties = loadProperties(LOCAL_FS_CONFIG_DIR);
		sqlProperties = loadProperties(SQL_CONFIG_DIR);	
		messageProperties=loadProperties(MESSAGE_DIR);
		clusterNodes=new ArrayList<JFSNode>();
		//localNode=JFSNodeConfigProper.getProper();
		//initalLocalFSInfo();
	}

	public static String getSql(String key) {
		return sqlProperties.getProperty(key);
	}

	public static String getLocalFSConfig(String key) {
		return localFSProperties.getProperty(key);
	}

	public static String getMessage(String key){
		return messageProperties.getProperty(key);
	}
	
	public static List<JFSNode> getClusterNodes() {
		return clusterNodes;
	}

	public static void setClusterNodes(List<JFSNode> clusterNodes) {
		Config.clusterNodes = clusterNodes;
	}
	
//	public static void configLocalFS() {
//		OutputStream fos;
//		try {
//			File prop=new File("/home/jairus/workspace/JFS/src/config/jfsnode/localfsconfig.properties");
//			System.out.println(prop.exists());
//			fos = new FileOutputStream(prop);
//			localFSProperties.setProperty("hostname", "hostname");
//			localFSProperties.store(fos, "ff");
//			fos.close();
//		} catch (Exception e) {
//			System.out.println("fail to save the config file :"+Config.class.getClassLoader().getResource(LOCAL_FS_CONFIG_DIR)+"/localfsconfig.properties");
//			e.printStackTrace();
//		}
//	}

	private static Properties loadProperties(String subDir) {
		Properties props = new Properties();
		URL configDir = Config.class.getClassLoader().getResource(subDir);
		System.out.println(configDir);
		File file = new File(configDir.getPath().replace("%20", " "));
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File f = files[i];
			System.out.println(f.getName());
			InputStream in = null;
			try {
				in = new FileInputStream(f);
				props.load(in);
			} catch (Exception e) {
				e.printStackTrace();
				LoggerFactory.getLogger().error(
						String.format("couldn't load the config file:%s",
								file.getAbsolutePath()), e);
			} finally {
				try {
					in.close();
				} catch (Exception e1) {
					e1.printStackTrace();
					LoggerFactory.getLogger().error(
							String.format("couldn't close the config file:%s",
									file.getAbsolutePath()), e1);
				}
			}
		}
		return props;
	}
	
//	public static void  main(String arg[]){
////		loadProperties(LOCAL_FS_CONFIG_DIR);
////		System.out.println(getLocalFSConfig("hostname"));
//		config();
//		System.out.println(getLocalFSConfig("hostname"));
//		configLocalFS();
//		config();
//		System.out.println(getLocalFSConfig("hostname"));
//	}
}
